<?php

namespace app\admin\controller;

use app\admin\library\module\Server;
use app\admin\model\User;
use app\common\model\Attachment;
use ba\Date;
use thinkct\controller\Backend;

class Dashboard extends Backend
{
    public function initialize(): void
    {
        parent::initialize();
    }

    public function index(): void
    {
        $installed = Server::installedList(root_path() . 'modules' . DIRECTORY_SEPARATOR);

        // 今日会员注册量
        $user = User::whereTime('create_time', 'today')->count();
        // 附件上传量
        $attachment = Attachment::count();
        // 会员总数
        $total = User::count();
        // 已装插件数
        $modules = count($installed);
        // 新注册用户
        $userList = User::where('status', 'enable')
            ->field('id,username,nickname,avatar,create_time')
            ->withAttr('create_time_text', function ($value, $data) {
                return Date::human($data['create_time']);
            })
            ->append(['create_time_text'])
            ->order('id desc')
            ->limit(12)
            ->select();

        // 返回数据
        $this->success('', [
            'remark' => get_route_remark(),
            'userList' => $userList,
            'count' => [
                'userRegNumber' => $user,
                'fileNumber' => $attachment,
                'usersNumber' => $total,
                'addonsNumber' => $modules
            ]
        ]);
    }
}